﻿using FireChat.Server.Session.Actions;
using FireChat.Share.Web.Actions;
using FireChat.Share.Web.Package;
using FireChat.Share.Web.Util;

namespace FireChat.Server.Session.Receiver;


/// <summary>
/// 远程客户端扩展方法
/// </summary>
internal static class PackageScaffold
{
    /// <summary>
    /// 构建该类型的数据包
    /// </summary>
    /// <param name="id">数据包Id</param>
    /// <param name="isSuccess"></param>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static ServerPackage CreatePackage(this ClientActions type, bool isSuccess, string message, object? data = null)
    {
        return new ServerPackage()
        {
            IsSuccess = isSuccess,
            Message = message,
            Data = data,
            ActionType = type
        };
    }

    /// <summary>
    /// 执行动作
    /// </summary>
    /// <param name="receiver"></param>
    public static void ExcuteAction(this PackageReceiver receiver)
    {
        var action = ActionScaffold<IServerAction>.Actions.FirstOrDefault(x => x.Type == receiver.Package.ActionType);
        var package = action?.Excute(receiver);

        if (package == null) return;

        _ = receiver.Session.SendAsync(package);
    }



    /// <summary>
    /// 回复一个包
    /// </summary>
    /// <param name="package"></param>
    public static ServerPackage ReplyAsync(this PackageReceiver receiver, ServerPackage package)
    {
        package.Id = receiver.Package.Id;
        return package;
    }

    /// <summary>
    /// 回复一个响应
    /// </summary>
    /// <param name="isSuccess">是否成功响应</param>
    /// <param name="message">备注消息</param>
    /// <param name="data">备注数据</param>
    public static ServerPackage ReplyAsync(this PackageReceiver receiver, bool isSuccess, string message, object? data = null)
    {
        var package = ClientActions.Response.CreatePackage(isSuccess, message, data);
        return receiver.ReplyAsync(package);
    }

    /// <summary>
    /// 回复一个成功响应
    /// </summary>
    /// <param name="message">备注消息</param>
    /// <param name="data">备注数据</param>
    public static ServerPackage ReplySuccessAsync(this PackageReceiver receiver, string message = nameof(ReplySuccessAsync), object? data = null)
    {
        return receiver.ReplyAsync(true, message, data);
    }

    /// <summary>
    /// 回复一个失败响应
    /// </summary>
    /// <param name="message">备注消息</param>
    /// <param name="data">备注数据</param>
    public static ServerPackage ReplyFailureAsync(this PackageReceiver receiver, string message = nameof(ReplyFailureAsync), object? data = null)
    {
        return receiver.ReplyAsync(false, message, data);
    }

    /// <summary>
    /// 回复一个响应
    /// </summary>
    /// <param name="isSuccess">成功条件</param>
    /// <param name="successMsg">成功消息</param>
    /// <param name="failureMsg">失败消息</param>
    /// <param name="data">附加数据</param>
    public static ServerPackage ReplyExpressAsync(this PackageReceiver receiver, bool isSuccess, string successMsg, string failureMsg, object? data = null)
    {
        return isSuccess ? receiver.ReplySuccessAsync(successMsg, data) : receiver.ReplyFailureAsync(failureMsg, data);
    }
}